package org.lex.keytool.ui;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.io.Writer;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSplitPane;

import org.lex.keytool.Swings;
import org.lex.keytool.ui.common.TitledMessagePane;

public class ProcessOutputPane extends JPanel {
	public static void main(String[] args) {
		Swings.show(new ProcessOutputPane());
	}

	private JSplitPane splitPane;
	private TitledMessagePane outMessagePane;
	private TitledMessagePane errMessagePane;

	private JProgressBar progressBar;
	private JPanel buttonsPane;
	private JButton stopButton;
	private JButton closeButton;

	public ProcessOutputPane() {
		initComponents();
	}

	public Writer getStdoutWriter() {
		return this.outMessagePane.getWriter();
	}

	public Writer getStderrWriter() {
		return this.errMessagePane.getWriter();
	}

	public void setProcessFinished(boolean b) {
		if (b) {
			this.stopButton.setEnabled(false);
			progressBar.setIndeterminate(false);
			progressBar.setValue(progressBar.getMaximum());
		}
	}

	private void initComponents() {
		GridBagConstraints gc;
		setLayout(new GridBagLayout());

		splitPane = new JSplitPane();
		splitPane.setDividerSize(6);
		splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
		splitPane.setResizeWeight(0.7);

		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 0;
		gc.fill = GridBagConstraints.BOTH;
		gc.weightx = 10.0;
		gc.weighty = 10.0;
		add(splitPane, gc);

		outMessagePane = new TitledMessagePane("Message");
		splitPane.setTopComponent(outMessagePane);

		errMessagePane = new TitledMessagePane("Error");
		splitPane.setBottomComponent(errMessagePane);

		progressBar = new JProgressBar();
		progressBar.setIndeterminate(true);
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 1;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.weightx = 10.0;
		add(progressBar, gc);

		FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 5, 5);
		fl.setAlignOnBaseline(true);
		buttonsPane = new JPanel(fl);
		gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 2;
		gc.fill = GridBagConstraints.HORIZONTAL;
		gc.weightx = 10.0;
		add(buttonsPane, gc);

		stopButton = new JButton();
		stopButton.setText("Stop");
		buttonsPane.add(stopButton);

		closeButton = new JButton();
		closeButton.setText("Close");
		buttonsPane.add(closeButton);
	}
}
